/**
 * @author jh
 */


import { observable, action, toJS } from 'mobx';
import { message, Modal } from 'antd';
import {WeaLocaleProvider, WeaTools} from 'ecCom';
import { WeaForm } from 'comsMobx';
import * as API from './../apis/userDefaultSettingApi';

const getLabel = WeaLocaleProvider.getLabel;

export default class UserPhraseEditStore {

    @observable visiable = false;
    @observable phraseDescValue = '';
    @observable saveDisable = false;
    @observable useTextAreaSignInput = 0;
    @observable isRequired = "required";
    @observable success = -1;  //-1无权限；0异常；1成功
    @observable form = new WeaForm();
    @observable loading = false;
	@observable phraseEditDialogTitle='';
    conditions = [];

    phraseId = '';

    constructor() {
        this.onEdit = this.onEdit;
        this.onCancelEdit = this.onCancelEdit;
        this.richTextOnChange = this.richTextOnChange;
		this.loadLabels = this.loadLabels;
        this.loadLabels();
    }

	loadLabels = ()=>{
        WeaTools.callApi('/api/workflow/label/loadPhraseEditLabels', 'GET', {}).then(data => {
            this.phraseEditDialogTitle = data.phraseEditDialogTitle;
        });
    }

    /**
     * 获取设置信息
     * @param id
     */
    onEdit = (id = '', defaultRemark) =>  {
        const para = {
            id: id
        }
        this.loading=true;
        API.doEdit(para).then((reVal) => {
            this.loading=false;
            this.form.resetForm();
            this.form.initFormFields(reVal.conditions);
            const phraseDescValue = defaultRemark ? defaultRemark : reVal.phraseDescValue;
            this.isRequired = phraseDescValue === '' ? 'required' : '';
            this.setStates({
                conditions:reVal.conditions,
                phraseDescValue:phraseDescValue,
                visiable:true,
                phraseId :id,
                success: reVal.success,
                useTextAreaSignInput : reVal.useTextAreaSignInput||0,
            })
        });
        
    }

    /**
     * 保存
     */
    onSave = (callFun)  => {
        if (this.realLength(this.phraseDescValue) > 4000) {
            Modal.warning({ title: getLabel(131329, '信息确认'), okText: getLabel(33703, '确认'), content: getLabel(385840, '文本长度不能超过4000(1个中文字符等于3个长度)') });
            return;
        }
        this.form.validateForm().then(f => {
            if (f.isValid) {
                this.saveDisable = true;
                const para = {
                    id: this.phraseId,
                    phraseDesc: this.phraseDescValue
                };
                const formPara = { ...this.form.getFormParams(), ...para };
                API.doSave(formPara).then((reVal) => {
                    message.success(reVal.msg);
                    this.saveDisable = false;
                    callFun && callFun({ workflowPhrases: formPara.phraseShort, workflowPhrasesContent: formPara.phraseDesc, status : 1, groupid : formPara.groupid, right : 1},this.phraseId);//.replace(/<[^>]+>/g, "")
                    this.onCancelEdit();
                });

            } else {
                f.showErrors();
            }
        });

    }
    clearStore=()=>{
        this.phraseDescValue = "";
        this.isRequired = "required";
        this.phraseId=""
    }
    /**
     * 关闭编辑弹框
     */
    onCancelEdit = () =>  {
        this.visiable = false;
        this.phraseDescValue = "";
        this.phraseId=""
    }

    /**
     * 富文本编辑器
     */
    richTextOnChange = (desc = '') =>  {
        this.phraseDescValue = desc;
    }

    realLength = (str) =>  {
        let j = 0;
        for (var i = 0; i <= str.length - 1; i++) {
            j = j + 1;
            if ((str.charCodeAt(i)) > 127) {
                j = j + 2;
            }
        }
        return j;
    }
    @action
    setStates = (states = {}) => {
        const keys = Object.keys(states);
        keys.forEach(key => {
            this[key] = states[key]
        })
    }
}